package com.cableex.mmb_mtj_android_v1;


import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ${Dota.Wang} on 2017/4/21.
 */
public class JsonStringUtils {
    private String TAG = "JsonStringUtils";
    //    private Map<String,String> jsonStringToMap = new HashMap<>();
    private String jsonString;
    private static final String TYPE_ID = "2";  //1 iPhone 2 Android
    private static String parameter;
    /***
     * 客户端调用的方法：传递参数向服务器中发送请求
     *
     * @param imei          设备号
     * @param appName       app名字
     * @param versionCode   应用的版本号
     * @param modelType     手机型号
     * @param event         事件类型
     * @param memberId      未登录传空、否则登录
     * @return
     */
    public static void login(final String post_hostURL, final String type, final String imei, final String appName, final String versionCode, final String modelType, final String event, final String memberId, final String KeyWords, final String method, final String productId, final String way ) {
        Map map = new HashMap();
        map.put("type", type);
        map.put("imei", imei);
        map.put("appName", appName);
        map.put("versionCode", versionCode);
        map.put("modelType", modelType);
        map.put("event", event);
        map.put("memberId", memberId);
        if (!TextUtils.isEmpty(KeyWords)) {
            map.put("keyWords", KeyWords);
        }
        if (!TextUtils.isEmpty(method)) {
            map.put("method", method);
        }
        if (!TextUtils.isEmpty(productId)) {
            map.put("productId", productId);
        }
        if (!TextUtils.isEmpty(way)) {
            map.put("way", way);
        }

        String json = new Gson().toJson(map);
        final String param = json.replaceAll("\"", "\'");

        Log.i("dd","json="+json);
        Log.i("dd","param="+param);

//            parameter =URLEncoder.encode(param, "UTF-8");



        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //post_hostURL   调试时改为UrlString.POST_HOST
                    URL urls = new URL(post_hostURL+UrlString.POST_BASE + URLEncoder.encode(param, "UTF-8"));
                    Log.i("get","urls="+urls);
                    HttpURLConnection urlConnection = (HttpURLConnection) urls.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    urlConnection.setReadTimeout(8000);
                    urlConnection.setConnectTimeout(8000);
                    //    urlConnection.setRequestProperty("Content-Type", "application/json");
                    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    // urlConnection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
                    urlConnection.setRequestProperty("Charset", "UTF-8");
                    urlConnection.connect();
                    DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
//                    dos.writeBytes(param);
                    dos.flush();
                    dos.close();


//                    int resultCode = urlConnection.getResponseCode();
                    if (urlConnection.getResponseCode() == 200) {
//                    if (HttpURLConnection.HTTP_OK == resultCode) {
                        StringBuffer sb = new StringBuffer();
                        String readLine = new String();
                        BufferedReader responseReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
                        while ((readLine = responseReader.readLine()) != null) {
                            sb.append(readLine).append("\n");
                        }
                        responseReader.close();
                        System.out.println(sb.toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}
